//
//  YJDPWDView.m
//  YJDSDK
//
//  Created by skyline on 2018/7/27.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "YJDPWDView.h"
@interface YJDPWDView()<YJDTextFieldDelegate>

@end
@implementation YJDPWDView
- (YJDButton *)titleIMG
{
    if (!_titleIMG)
    {
        _titleIMG = [[YJDButton alloc] init];
        _titleIMG.height = TITLE_HEIGHT;
        _titleIMG.style = YJDButtonTitleStyleCameo;
        _titleIMG.userInteractionEnabled = NO;
        [self addSubview:_titleIMG];
        [_titleIMG mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self.backBtn);
            make.size.mas_offset(CGSizeMake(70, TITLE_HEIGHT));
        }];
    }
    return _titleIMG;
}

- (YJDTextField *)firstTF
{
    if (!_firstTF)
    {
        _firstTF = [[YJDTextField alloc] init];
        _firstTF.type = YJDTextFieldTypePassword;
        _firstTF.customDelegate = self;
        _firstTF.returnKeyType = UIReturnKeyNext;
        [self addSubview:_firstTF];
        [_firstTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(GAP * 2.0 + BACKBUTTON_HEIGHT);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _firstTF;
}


- (YJDTextField *)secondTF
{
    if (!_secondTF)
    {
        _secondTF = [[YJDTextField alloc] init];
        _secondTF.type = YJDTextFieldTypePassword;
        _secondTF.customDelegate = self;
        _secondTF.returnKeyType = UIReturnKeyNext;
        [self addSubview:_secondTF];
        [_secondTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self.firstTF.mas_bottom).with.offset(GAP / 2.0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _secondTF;
}

- (YJDTextField *)thirdTF
{
    if (!_thirdTF)
    {
        _thirdTF = [[YJDTextField alloc] init];
        _thirdTF.type = YJDTextFieldTypePassword;
        [self addSubview:_thirdTF];
        [_thirdTF mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.top.equalTo(self.secondTF.mas_bottom).with.offset(GAP / 2.0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(self.bounds.size.width - GAP * 2, TEXTFILDHEIGHT));
        }];
    }
    return _thirdTF;
}

- (YJDButton *)findBtn
{
    if (!_findBtn)
    {
        _findBtn= [[YJDButton alloc] init];
        _findBtn.height = TEXTFILDHEIGHT;
        [_findBtn addTarget:self action:@selector(yjd_actionForFindButton) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_findBtn];
        [_findBtn mas_makeConstraints:^(SK_MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.thirdTF.mas_bottom).with.offset(GAP / 2.0);
            make.size.mas_offset(CGSizeMake((self.bounds.size.width - GAP * 2.0), BUTTONHEIGHT));
        }];
    }
    return _findBtn;
}

- (void)setType:(YJDPWDViewType)type
{
    if (_type != type)
    {
        _type = type;
        self.firstTF.placeholder = (_type == YJDPWDViewTypeFindPWD)?@"请输入用户名":@"请输入原密码";
        self.secondTF.placeholder = (_type == YJDPWDViewTypeFindPWD)?@"获取验证码":@"请输入新密码";
        self.thirdTF.placeholder = (_type == YJDPWDViewTypeFindPWD)?@"请输入新密码":@"请确认新密码";
        self.firstTF.type = (_type == YJDPWDViewTypeFindPWD)?YJDTextFieldTypeAccountNormal:YJDTextFieldTypePassword;
        self.secondTF.type = (_type == YJDPWDViewTypeFindPWD)?YJDTextFieldTypeVerify:YJDTextFieldTypePassword;
    }
}

- (void)yjd_actionForBackButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDPWDViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventBack dict:nil];
    }
}

- (void)yjd_actionForFindButton
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDPWDViewWithEvent:dict:)])
    {
        if (self.type == YJDPWDViewTypeFindPWD)
        {
            [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventFindPWD dict:@{@"name":self.firstTF.text,@"password":self.thirdTF.text}];
        }
        else if (self.type == YJDPWDViewTypeResetPWD)
        {
            [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventResetPWD dict:@{@"oldpassword":self.firstTF.text,@"password":self.thirdTF.text}];
        }
    }
}

#pragma mark - YJDTextFieldDelegate
- (BOOL)yjd_actionForVerifyCode
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_actionForYJDPWDViewWithEvent:dict:)])
    {
        [self.delegate yjd_actionForYJDPWDViewWithEvent:YJDPWDViewEventVerify dict:@{@"account":self.firstTF.text}];
        return ([self.firstTF.text yjd_isNumberOralphabet])?YES:NO;
    }
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if ([textField isEqual:self.firstTF])
    {
        [self.secondTF becomeFirstResponder];
        return NO;
    }
    else if([textField isEqual:self.secondTF])
    {
        [self.thirdTF becomeFirstResponder];
        return NO;
    }
    return YES;
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
